# 引入selenium库中的webdriver模块，实现对网页的操作
from selenium import webdriver
# 引入time模块，实现延时
import time
# 引入By Class,辅助元素定位
from selenium.webdriver.common.by import By
# 引入ActionChains Class, 辅助鼠标移动、
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from Unit1 import Unit1
from Unit2 import Unit2
from Unit3 import Unit3
from Unit4 import Unit4
from Unit5 import Unit5
from Unit6 import Unit6
from Unit7 import Unit7
from Unit8 import Unit8
from Unit9 import Unit9
from Unit10 import Unit10
def login(account, password):
    time.sleep(1)
    # 输入账号密码
    driver.find_element(By.XPATH, '//*[@id="user"]').send_keys(account)
    driver.find_element(By.XPATH, '//*[@id="pwd"]').send_keys(password)

    # 点击登录
    WebDriverWait(driver, 5, 0.5).until(
        EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div/div/div/button[1]')))
    driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div/div/div/button[1]').click()

    # 点击大学生听力提高级
    WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="iframepage"]')))
    iframe_label = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe_label)
    WebDriverWait(driver, 5, 0.5).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="div_book"]/div[2]/div[1]/div[1]/a[2]')))
    driver.find_element(By.XPATH, '//*[@id="div_book"]/div[2]/div[1]/div[1]/a[2]').click()

def fill_out(driver):
    Unit1(driver)
    print(1)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit2(driver)
    print(2)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit3(driver)
    print(3)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit4(driver)
    print(4)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit5(driver)
    print(5)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit6(driver)
    print(6)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit7(driver)
    print(7)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit8(driver)
    print(8)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit9(driver)
    print(9)
    iframe = driver.find_element(By.XPATH, '//*[@id="iframepage"]')
    driver.switch_to.frame(iframe)

    Unit10(driver)
    print(10)

if __name__ == '__main__':

    # 读取账号密码
    file_name = 'account.txt'
    with open(file_name, 'r') as file_object:
        accounts = file_object.readlines()
    for line in accounts:
        # 打开浏览器
        driver = webdriver.Edge()
        # 打开网页
        driver.get("http:/saas.51learning.com.cn/listen/loginnchu.html")

        # 全屏展示
        driver.maximize_window()
        time.sleep(1)
        account = line.split()[0]
        password = line.split()[1]
        login(account, password)
        fill_out(driver)
        print(account + '    ' + password)
        with open ('finish.txt', 'a') as account_finish:
            account_finish.write(account+" "+password)
        driver.quit()